package com.yuju.group.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.SeqCodeEnum;
import com.yuju.group.dao.IDepartmentDao;
import com.yuju.group.dao.entity.Department;
import com.yuju.group.model.DepartmentModel;
import com.yuju.group.model.EmployeeModel;
import com.yuju.group.model.HotelModel;
import com.yuju.group.service.ICodeGenService;
import com.yuju.group.service.IDepartmentService;
import com.yuju.group.service.IEmployeeService;
import com.yuju.group.service.IHotelService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：DepartmentServiceImpl  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年11月27日 上午10:33:48  <br />
 * @version : 1.1 <br />
 */
@Service
public class DepartmentServiceImpl extends AbstractBaseService<DepartmentModel, Department, String> implements IDepartmentService{

	@Autowired
	private IDepartmentDao departmentDao;
	@Autowired
	private IEmployeeService employeeService;
	@Autowired
	private IHotelService hotelService;
	@Autowired
	private ICodeGenService codeGenService;
	@Override
	public IBaseDao<DepartmentModel, Department, String> getDao() {
		// TODO Auto-generated method stub
		return departmentDao;
	}
	
	/**
	 * 
	 * 功能：<br/>新增部门
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午5:43:36 <br/>
	 */
	@Override
	public int save(DepartmentModel departmentModel){
		int returnValue = 0;
		for(int i= 0; i < departmentModel.getHotelIds().length; i++){
			departmentModel.setId(Utils.getUUID());
			departmentModel.setHotelId(departmentModel.getHotelIds()[i]);
			//部门编号
			String code = codeGenService.generate(departmentModel.getGroupId(), departmentModel.getHotelId(),null,SeqCodeEnum.DEPT_CODE.getCode());
			departmentModel.setCode(code);
			departmentModel.setGmtCreate(new Date());
			departmentModel.setGmtModified(new Date());
			returnValue += this.getDao().saveSelective(departmentModel);
		}
		return returnValue;
	}
	/**
	 * 
	 * 功能：<br/>部门更新
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午5:44:36 <br/>
	 */
	@Override
	public int updateSelective(DepartmentModel departmentModel){
		departmentModel.setGmtModified(new Date());
		return this.getDao().updateSelective(departmentModel);
	}

	/**
	 * 
	 * 功能: 部门删除<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月30日 上午10:32:02<br/>
	 * 
	 * @param id
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#del(java.lang.Object)
	 */
	@Override
	public int del(String id){
		//首先判断该部门下是否有员工
		EmployeeModel employeeModel = new EmployeeModel();
		employeeModel.setDepartmentId(id);
		List<EmployeeModel> employeeList = employeeService.list(employeeModel);
		//判断员工集合是否为空，为空执行删除，不为空返回提示不可删除
		if(employeeList.size() != 0){
			return 0;
		}
		return this.getDao().del(id);
	}

	/**
	 * 
	 * 功能:重名校检 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月13日 上午10:36:11<br/>
	 * 
	 * @param departmentModel
	 * @return <br/>
	 * @see com.yuju.group.service.IDepartmentService#selectByName(com.yuju.group.model.DepartmentModel)
	 */
	@Override
	public String selectByName(DepartmentModel departmentModel) {
		//定义返回值
		boolean result = false;
		String hotelNames = "";
		
		 DepartmentModel model = new DepartmentModel();
		 model.setName(departmentModel.getName());
		 model.setGroupId(departmentModel.getGroupId());
		//修改时验证重名
		 if (Utils.isNotNull(departmentModel.getId())) {
             DepartmentModel departmentModel1 = convert(this.getDao().getObj(departmentModel.getId()));
             if (departmentModel.getName().equals(departmentModel1.getName())) {
                 result = true;
             } else {
            	 Map<Object, Object> beanMap = getBeanMap(model);
                 List<DepartmentModel> list = this.getDao().list(beanMap);
                 if (Utils.isNull(list)) {
                     result = true;
                 }
             }
          //新增验证重名
		 } else {
			 int a = 0;
			 String[] hotelIds = departmentModel.getHotelIds();
             for (int i=0; i<hotelIds.length; i++){
                 model.setHotelId(hotelIds[i]);
                 Map<Object, Object> beanMap = getBeanMap(model);
                 List<DepartmentModel> list = this.getDao().list(beanMap);
                 if (list.size() == 0) {
                     result = true;
                 }else {
                     a++;
                     HotelModel hotelModel = hotelService.findById(hotelIds[i]);
                     hotelNames = hotelNames + hotelModel.getName() + "&";
                 }
             }
             if(a != 0){
                 result = false;
             }
		 }
		 Map<String, Object> map = new HashMap<>();
         map.put("valid", result);
         map.put("hotelNames",hotelNames);
         
         ObjectMapper mapper = new ObjectMapper();
         String returnString = "";
		try {
			returnString = mapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnString;
	}
	
}
